package com.elai.operate.service.impl;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.elai.common.constant.ScheduleConstants;
import com.elai.common.utils.DateUtils;
import com.elai.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.elai.operate.mapper.OperateEquityMapper;
import com.elai.operate.domain.OperateEquityDO;
import com.elai.operate.service.OperateEquityService;

/**
 * 权益绑定Service业务层处理
 *
 * @author elai
 * @date 2021-04-04
 */
@Service
public class OperateEquityServiceImpl implements OperateEquityService
{
    @Autowired
    private OperateEquityMapper operateEquityMapper;

    /**
     * 查询权益绑定
     *
     * @param id 权益绑定ID
     * @return 权益绑定
     */
    @Override
    public OperateEquityDO selectOperateEquityById(Long id)
    {
        return operateEquityMapper.selectOperateEquityById(id);
    }

    /**
     * 查询权益绑定列表
     *
     * @param operateEquity 权益绑定
     * @return 权益绑定
     */
    @Override
    public List<OperateEquityDO> selectOperateEquityList(OperateEquityDO operateEquity)
    {
        return operateEquityMapper.selectOperateEquityList(operateEquity);
    }

    /**
     * 新增权益绑定
     *
     * @param operateEquity 权益绑定
     * @return 结果
     */
    @Override
    public int insertOperateEquity(OperateEquityDO operateEquity)
    {
        operateEquity.setCreateTime(DateUtils.getNowDate());
        operateEquity.setStatus("01");
        int result= operateEquityMapper.insertOperateEquity(operateEquity);
        return result;
    }

    private JSONObject extractedJson(OperateEquityDO operateEquity) {
        String touchType= operateEquity.getTouchType();
        JSONObject jsonData=new JSONObject();
        //第一步
        JSONObject step1=new JSONObject();
        step1.put("name", operateEquity.getName());
        step1.put("title", operateEquity.getTitle());
        step1.put("content", operateEquity.getContent());
        step1.put("channel", operateEquity.getChannel());
        step1.put("touchType", operateEquity.getTouchType());
        JSONObject touchDate=new JSONObject();
        if("1".equals(touchType)){
            touchDate.put("touchDate", operateEquity.getTouchDate());
            touchDate.put("touchTime", operateEquity.getTouchTime());
            String []dateArray=operateEquity.getTouchDate().split("-");
            String []timeArray=operateEquity.getTouchTime().split(":");
            String cronExpression="0 "+timeArray[1]+" "+timeArray[0]+" "+dateArray[2]+" "+dateArray[1]+" ? "+dateArray[0];
            operateEquity.setCronExpression(cronExpression);
            touchDate.put("cronExpression",cronExpression);
        }else if("2".equals(touchType)){
            touchDate.put("touchDateType", operateEquity.getTouchDateType());
            touchDate.put("touchDateValue", operateEquity.getTouchDateValue());
            touchDate.put("touchDate", operateEquity.getTouchDate());
            touchDate.put("touchTime", operateEquity.getTouchTime());
            String beginStr=DateUtils.dateTime(operateEquity.getBeginDate())+" 00:00:00";
            operateEquity.setBeginDate(DateUtils.parseDate(beginStr));
            touchDate.put("beginDate",beginStr);

            String endStr=DateUtils.dateTime(operateEquity.getEndDate())+" 23:59:59";
            operateEquity.setEndDate(DateUtils.parseDate(endStr));
            touchDate.put("endDate",endStr);
            touchDate.put("partLimit", operateEquity.getPartLimit());
            if("2".equals(operateEquity.getPartLimit())){
                touchDate.put("partLimitDays", operateEquity.getPartLimitDays());
                touchDate.put("partLimitTimes", operateEquity.getPartLimitTimes());
            }
            Date beginDate= operateEquity.getBeginDate();
            LocalDate begin=LocalDate.parse(DateUtils.parseDateToStr("yyyy-MM-dd",beginDate));
            Date endDate1= operateEquity.getEndDate();
            LocalDate endDate= LocalDate.parse(DateUtils.parseDateToStr("yyyy-MM-dd",endDate1));
            String []timeArray=operateEquity.getTouchTime().split(":");
            String cronExpression="";
            if("1".equals(operateEquity.getTouchDateType())){
                cronExpression="0 "+timeArray[1]+" "+timeArray[0]+" "+begin.getDayOfMonth()+"-"+ endDate.getDayOfMonth()+" "+begin.getMonthValue()+"-"+ endDate.getMonthValue()+" ? "+"*";
            }
            else if("2".equals(operateEquity.getTouchDateType())){
                cronExpression="0 "+timeArray[1]+" "+timeArray[0]+" ? "+begin.getMonthValue()+"-"+ endDate.getMonthValue()+" "+operateEquity.getTouchDateValue();
            }
            else if("3".equals(operateEquity.getTouchDateType())){
                cronExpression="0 "+timeArray[1]+" "+timeArray[0]+" "+operateEquity.getTouchDateValue().replace("号","")+" "+begin.getMonthValue()+"-"+ endDate.getMonthValue()+" ? ";
            }
            operateEquity.setCronExpression(cronExpression);
            touchDate.put("cronExpression",cronExpression);
        }
        step1.put("touchDateRule",touchDate);
        operateEquity.setStep1Json(step1.toString());
        jsonData.put("step1",step1);
        //第二步
        JSONObject step2=new JSONObject();
        step2.put("personType", operateEquity.getPersonType());
        String personTags= operateEquity.getPersonTags();
        JSONArray array=new JSONArray();
        if(StringUtils.isNotEmpty(personTags)){
            String [] tags=personTags.split(",");
            for(String id:tags){
                JSONObject person=new JSONObject();
                person.put("tagName","tagName");
                person.put("tagValue",id);
                array.add(person);
            }
        }
        step2.put("personTags",array);
        operateEquity.setStep2Json(step2.toString());
        jsonData.put("step2",step2);

        //第三步
        JSONObject step3=new JSONObject();
        step3.put("translateTime", operateEquity.getTranslateTime());
        step3.put("translateType", operateEquity.getTranslateType());

        JSONArray ruleAry=(JSONArray) JSONObject.parse(operateEquity.getRuleContent());
        JSONArray ruleArray=new JSONArray();
        for(int i=0;i<ruleAry.size();i++){
            JSONObject obj=(JSONObject)ruleAry.get(i);
            JSONObject rule=new JSONObject();
            rule.put("eventType",obj.getString("eventType"));
            rule.put("eventKey",obj.getString("eventKey"));
            rule.put("eventValue",obj.getString("eventValue"));
            ruleArray.add(rule);
        }
        step3.put("roleContent",ruleArray );
        operateEquity.setStep3Json(step3.toString());
        jsonData.put("step3",step3);
        return jsonData;
    }

    /**
     * 修改权益绑定
     *
     * @param operateEquity 权益绑定
     * @return 结果
     */
    @Override
    public int updateOperateEquity(OperateEquityDO operateEquity)
    {
        return operateEquityMapper.updateOperateEquity(operateEquity);
    }

    /**
     * 批量删除权益绑定
     *
     * @param ids 需要删除的权益绑定ID
     * @return 结果
     */
    @Override
    public int deleteOperateEquityByIds(Long[] ids)
    {
        return operateEquityMapper.deleteOperateEquityByIds(ids);
    }

    /**
     * 删除权益绑定信息
     *
     * @param id 权益绑定ID
     * @return 结果
     */
    @Override
    public int deleteOperateEquityById(Long id)
    {
        return operateEquityMapper.deleteOperateEquityById(id);
    }
}
